package com.hydl.power.controller;

import com.github.pagehelper.PageInfo;
import com.hydl.power.aop.annotation.LogAnnotation;
import com.hydl.power.mapper.CityMapper;
import com.hydl.power.mapper.PowerRoomMapper;
import com.hydl.power.mapper.ProjectMapper;
import com.hydl.power.pojo.*;
import com.hydl.power.service.DeviceService;
import com.hydl.power.service.PowerRoomService;
import com.hydl.power.service.SysUserService;
import com.hydl.power.utils.Resp;
import com.hydl.power.utils.TableUtils;
import io.micrometer.core.instrument.util.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(value = "PowerRoomController", tags = "配电房")
@RestController
@RequestMapping("/powerRoom")
@CrossOrigin
public class PowerRoomController {
    @Autowired
    private PowerRoomService powerRoomService;
    @Autowired
    private ProjectMapper projectMapper;
    @Autowired
    private CityMapper cityMapper;
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private DeviceService deviceService;


    //@LogAnnotation(module = "配电房",description = "配电房列表")
    @ApiOperation("配电房列表")
    @PostMapping("/powerRoomList")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "pageNum", value = "页码", required = false, dataType = "Integer"),
            @ApiImplicitParam(paramType = "query", name = "pageSize", value = "显示个数", required = false, dataType = "Integer")
    })
    public Resp<PageInfo<PowerRoom>> powerRoomList(@RequestParam(required = true )Integer pageNum,
                                                       @RequestParam(required = true )Integer pageSize) {
        PageInfo<PowerRoom>  list=powerRoomService.selectByConditions(pageNum,pageSize);
        return Resp.success(list);
    }

    @LogAnnotation(module = "配电房",description = "删除配电房")
    @ApiOperation("删除配电房")
    @PostMapping("/delPowerRoom")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id", value = "配电房ID", dataType = "Integer")
    })
    public Resp delPowerRoom(Integer id) {
        PowerRoom powerRoom =powerRoomService.getAllById(id);
        int count = deviceService.selectCountByRoomId(powerRoom.getRoomId());
        if(count>0){
            return Resp.fail("删除失败，配电房下还有设备未删除");
        }
        int i =  powerRoomService.delPowerRoom(id);
        if (i == 1){
            return Resp.success("删除成功");
        }else{
            return Resp.fail("删除失败");
        }
    }


    @LogAnnotation(module = "配电房",description = "添加配电房")
    @ApiOperation("添加配电房")
    @PostMapping("/addPowerRoom")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "roomName", value = "配电房名称", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "address", value = "配电房地址", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "areaId", value = "城市id", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "projectId", value = "项目id", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "highVoltageBoard", value = "高压柜", required = true, dataType = "Integer"),
            @ApiImplicitParam(paramType = "query", name = "lowPressureTank", value = "低压柜", required = true, dataType = "Integer")
    })
    public Resp addPowerRoom(String roomName,String address,String areaId,String projectId,Integer highVoltageBoard,Integer lowPressureTank) {
        if(StringUtils.isBlank(roomName) || StringUtils.isBlank(address) || StringUtils.isBlank(areaId) || StringUtils.isBlank(projectId)){
            return Resp.fail("参数不能为空");
        }
        if(projectMapper.queryProjectByProId(projectId)== null){
            return Resp.success("输入的项目编码未查询到信息");
        }
        if(cityMapper.queryCityByCityId(areaId)== null){
            return Resp.success("输入的城市编码未查询到信息");
        }
        int i = powerRoomService.addPowerRoom(roomName,address,areaId,projectId,highVoltageBoard,lowPressureTank);
        if (i == 1){
            return Resp.success("添加成功");
        }else{
            return Resp.fail("添加失败");
        }
        //return Resp.success(powerRoomService.addPowerRoom(roomName,navId,address));
    }


    //@LogAnnotation(module = "配电房",description = "修改配电房")
    @ApiOperation("修改配电房")
    @PostMapping("/updatePowerRoomById")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id", value = "配电房ID",  dataType = "Integer"),
            @ApiImplicitParam(paramType = "query", name = "roomName", value = "配电房名称", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "address", value = "配电房地址", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "highVoltageBoard", value = "高压柜", required = false, dataType = "Integer"),
            @ApiImplicitParam(paramType = "query", name = "lowPressureTank", value = "低压柜", required = false, dataType = "Integer")
    })
    public Resp updatePowerRoomById(Integer id,String roomName,String address,String highVoltageBoard,String lowPressureTank) {
        if(StringUtils.isBlank(roomName) || StringUtils.isBlank(roomName)){
            return Resp.fail("参数不能为空");
        }
        int i =  powerRoomService.updatePowerRoomById(id, roomName, address,highVoltageBoard,lowPressureTank);
        if (i == 1){
            return Resp.success("修改成功");
        }else{
            return Resp.fail("修改失败");
        }
    }


    // @LogAnnotation(module = "配电房",description = "根据ID获取配电房")
    @ApiOperation(value="根据ID获取配电房")
    @PostMapping("/getPowerRoomById")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id", value = "配电房id", dataType = "Integer"),
    })
    public Resp getPowerRoomById(Integer id){
        PowerRoom powerRoom=powerRoomService.getAllById(id);
        if(powerRoom == null ){
            return Resp.success("未查询到信息",null);
        }
        return Resp.success(powerRoom);
    }


    //@LogAnnotation(module = "配电房",description = "按条件查询配电房")
    @ApiOperation(value = "按条件查询配电房")
    @PostMapping(value = "/selectByCityId")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "pageNum", value = "页码", required = false, dataType = "Integer"),
            @ApiImplicitParam(paramType = "query", name = "pageSize", value = "显示个数", required = false, dataType = "Integer"),
            @ApiImplicitParam(paramType = "query", name = "cityId", value = "城市id", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "projectId", value = "项目id", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "token", value = "token", required = false, dataType = "String"),
    })
    public Resp<PageInfo<PowerRoom>> selectByCityId(@RequestParam(required = true )Integer pageNum,
                                                            @RequestParam(required = true )Integer pageSize,
                                                            @RequestParam(required = false) String cityId,
                                                            @RequestParam(required = false) String projectId,
                                                            @RequestParam(required = false) String token){

        SysUser sysUser = sysUserService.getUserByToken(token);
        if(sysUser.getStatus() == 2 ){
            PageInfo<PowerRoom> list=powerRoomService.selectByTokenId(pageNum,pageSize,cityId,projectId,token);
            return Resp.success(list);
        }
        PageInfo<PowerRoom>  list=powerRoomService.selectByCityId(pageNum,pageSize,cityId,projectId);
        return Resp.success(list);
    }
}
